﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using BI.CF.Model;
using BI.CF.IDAL;
using System.Data.SqlClient;
using BI.CF.PageList;

namespace BI.CF.DAL
{
    /// <summary>
    /// 登陆信息DAL
    /// </summary>
    public class LoginRecord_DAL : LoginRecordIDAL
    {
        /// <summary>
        /// 添加登陆记录信息
        /// </summary>   
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddLoginRecord(LoginRecordModel model)
        {
            SqlParameter[] parameter = {
                                        new SqlParameter("@LIP",SqlDbType.VarChar,18),
                                        new SqlParameter("@LTime",SqlDbType.DateTime),
                                        new SqlParameter("@OperationRsult",SqlDbType.VarChar,50),
                                        new SqlParameter("@UserId",SqlDbType.Int)
                                      };
            parameter[0].Value = model.LIP;
            parameter[1].Value = model.LTime;
            parameter[2].Value = model.OperationRsult;
            parameter[3].Value = model.UserId;
            return DbHelperSQL.RunProcedureReturnBool("PROC_LoginRecordAdd", parameter);
        }

        /// <summary>
        /// 分页显示IP信息
        /// </summary>
        /// <param name="TimeMin">从这个时间开始</param>
        /// <param name="TimeMax">到这个时间结束</param>
        /// <param name="state">用户登录状态</param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public PageList.Pager<LoginRecordModel> ShowLoginRecord(string TimeMin, string TimeMax, string state, int Uid, int PageIndex, int PageSize)
        {
            SqlParameter[] parameter ={
                                         new SqlParameter("@Pharmacy_TimeMin",SqlDbType.VarChar),
                                         new SqlParameter("@Pharmacy_TimeMax",SqlDbType.VarChar),
                                         new SqlParameter("@OperationRsult",SqlDbType.VarChar),
                                         new SqlParameter("@UserId",SqlDbType.Int),
                                         new SqlParameter("@PageIndex",SqlDbType.Int),
                                         new SqlParameter("@PageSize",SqlDbType.Int)
                                     };
            parameter[0].Value = TimeMin;
            parameter[1].Value = TimeMax;
            parameter[2].Value = state;
            parameter[3].Value = Uid;
            parameter[4].Value = PageIndex;
            parameter[5].Value = PageSize;
            List<LoginRecordModel> list = new List<LoginRecordModel>();
            int totalCount = 0;
            DataTable dt = DbHelperSQL.RunProcedure("PROC_LoginRecordShow", parameter, PageSize, PageIndex, out totalCount);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                LoginRecordModel model = new LoginRecordModel();

                model.LRId = Convert.ToInt32(dt.Rows[i]["LRId"].ToString());
                model.LIP = dt.Rows[i]["LIP"].ToString();
                model.LTime = Convert.ToDateTime(dt.Rows[i]["LTime"]);
                model.OperationRsult = dt.Rows[i]["OperationRsult"].ToString();
                model.UserId = Convert.ToInt32(dt.Rows[i]["UserId"].ToString());
                list.Add(model);
            }
            Pager<LoginRecordModel> page = new Pager<LoginRecordModel>();
            page.Data = list;
            page.PageIndex = PageIndex;
            page.PageSize = PageSize;
            page.PageCount = (int)Math.Ceiling(Convert.ToDecimal(totalCount) / Convert.ToDecimal(PageSize));
            return page;
        }

    }
}
